package com.qfedu.entity.course03;

/**
 *
 */
public class SqStack<E> {
    private E[] elements;
    private int top;

    public SqStack(int size) {
        elements = (E[]) new Object[size];
        top = 0;
    }

    public int push(E e) {
        if (top == elements.length) {
            //栈满
            return -1;
        }
        //新值放入栈顶指针所在位置
        elements[top] = e;
        //栈顶指针+1
        top++;
        return 1;
    }

    public E pop() {
        if (top == 0) {
            //空栈
            return null;
        }
        //栈顶指针下移一个位置 -1
        top--;
        //取栈顶元素
        return elements[top];
    }

    public E get() {
        if (top == 0) {
            //空栈
            return null;
        }
        //取栈顶元素
        return elements[top - 1];
    }

    public boolean isEmpty() {
        //空栈
        return top == 0;
    }
}
